<?php

namespace app\controller\admin\cnf;

use app\bll\Hook as BllHook;
use app\bll\UserIntegralRule as BllUserIntegralRule;
use core\controller\AdminBase;
use Exception;
use support\View;

class UserIntegralRule extends AdminBase
{
    public function index()
    {
        $bllUserIntegralRule = new BllUserIntegralRule();
        $rules = $bllUserIntegralRule->paginator(title: request()->get('title', ''));
        View::assign('rules', $rules);
        return  $this->view();
    }


    public function add()
    {
        if (is_post()) {
            try {
                $bllUserIntegralRule = new BllUserIntegralRule();
                $bllUserIntegralRule->add(request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {
            $bllHook = new BllHook();
            $actions = $bllHook->cascader();
            View::assign('actions', $actions);
            return $this->view();
        }
    }


    public function edit()
    {
        $id = request()->get('id');
        $bllUserIntegralRule = new BllUserIntegralRule();

        if (is_post()) {
            try {
                $bllUserIntegralRule = new BllUserIntegralRule();
                $bllUserIntegralRule->edit($id, request()->post());
            } catch (Exception $ex) {
                return $this->error($ex->getMessage());
            }
            return $this->success();
        } else {

            $ruleinfo = $bllUserIntegralRule->find($id);
            $bllHook = new BllHook();
            $actions = $bllHook->cascader();
            View::assign('actions', $actions);
            View::assign('ruleinfo', $ruleinfo);
            return $this->view();
        }
    }


    public function delete()
    {
        try {
            $bllUserIntegralRule = new BllUserIntegralRule();
            $bllUserIntegralRule->delete(request()->get('id'));
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
